﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using RayD.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.SystemModule;
using RayD.WebApi.Models;
using RayD.Repository;
using RayD.Repository.Core;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 数据字典资源
    /// </summary>
    public class DataItemController : BaseAdminController
    {
        #region 构造函数注入
        private DataItemApp _DataItem;
        public DataItemController(DataItemApp DataItem)
        {
            this._DataItem = DataItem;
        }
        #endregion

        #region 参数实体
        /// <summary>
        /// 分页数据查询响应参数
        /// </summary>
        public class GetDetailPageRespone
        {
            /// <summary>
            /// 查询响应数据
            /// </summary>
            public IEnumerable<DataItemDetailEntity> rows { get; set; }
            /// <summary>
            /// 总记录数
            /// </summary>
            public int records { get; set; }
        }
        #endregion

        #region 获取数据字典
        /// <summary>
        /// 获取数据字典（树型）
        /// </summary>
        /// <param name="B_ItemCode">分类编码</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<IEnumerable<DataItemDetailEntity>> GetTreeList(string B_ItemCode)
        {
            var list = _DataItem.GetDetailListByCode(B_ItemCode);
            list.ForEach(c => c.value = c.B_ItemValue);
            return Success(list.ToTree());
        }
        #endregion

        #region 获取数据 - 字典分类
        /// <summary>
        /// 获取树型列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<DataItemHeadEntity>> GetHeadTreeList(string queryjson)
        {
            var list = _DataItem.GetHeadTreeList(queryjson);
            return Success(list);
        }
        #endregion

        #region 获取数据 - 字典明细
        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetDetailPageRespone> GetDetailPageList(string queryjson, string pagination)
        {
            var paginationObj = pagination.ToObject<Pagination>();
            var data = _DataItem.GetDetailPageList(queryjson, paginationObj);
            return Success(new GetDetailPageRespone()
            {
                rows = data,
                records = paginationObj.records
            });
        }
        /// <summary>
        /// 获取树型列表数据
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<DataItemDetailEntity>> GetDetailTreeList(string id)
        {
            var list = _DataItem.GetDetailTreeList(id);
            return Success(list);
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体(新增,编辑)
        /// </summary>
        /// <param name="request">字典分类实体</param>
        /// <returns></returns>
        [HttpPut, Logging("数据字典分类保存")]
        public ResParameter SaveHead(DataItemHeadEntity request)
        {
            var (isSuccess, msg) = _DataItem.SaveHead(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="request">字典分类实体</param>
        /// <returns></returns>
        [HttpPut, Logging("数据字典分类删除")]
        public ResParameter DeleteHead(DataItemHeadEntity request)
        {
            var (isSuccess, msg) = _DataItem.DeleteHead(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }

        /// <summary>
        /// 保存实体(新增,编辑)
        /// </summary>
        /// <param name="request">字典明细实体</param>
        /// <returns></returns>
        [HttpPut, Logging("数据字典明细保存")]
        public ResParameter SaveDetail(DataItemDetailEntity request)
        {
            var (isSuccess, msg) = _DataItem.SaveDetail(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="request">字典明细实体</param>
        /// <returns></returns>
        [HttpPut, Logging("数据字典明细删除")]
        public ResParameter DeleteDetail(DataItemDetailEntity request)
        {
            var (isSuccess, msg) = _DataItem.DeleteDetail(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 状态更改
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns></returns>
        [HttpPut, Logging("数据字典明细状态更改")]
        public ResParameter ChangeState(DataItemDetailEntity request)
        {
            var (isSuccess, msg) = _DataItem.ChangeState(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        #endregion

        #region 数据验证
        /// <summary>
        /// 验证字段值是否存在
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <param name="field">字段名</param>
        /// <param name="value">字段值</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter ValidateHeadExist(string id, string field, string value)
        {
            var res = _DataItem.ValidateHeadExist(id, field, value);
            if (!res)
            {
                return Success("校验通过");
            }
            else
            {
                return Fail("值已存在，校验不通过！");
            }
        }
        /// <summary>
        /// 验证字段值是否存在
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <param name="field">字段名</param>
        /// <param name="value">字段值</param>
        /// <param name="itemId">分类主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter ValidateDetailExist(string id, string field, string value, string itemId)
        {
            var res = _DataItem.ValidateDetailExist(id, field, value, itemId);
            if (!res)
            {
                return Success("校验通过");
            }
            else
            {
                return Fail("值已存在，校验不通过！");
            }
        }
        #endregion
    }
}
